package com.tastesport.chat.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tastesport.chat.mapper.RedpacketGetMapper;
import com.tastesport.chat.mapper.RedpacketMapper;
import com.tastesport.chat.model.redpacket.Redpacket;
import com.tastesport.chat.model.redpacket.RedpacketGet;
import com.tastesport.chat.model.redpacket.QueryParam;
import com.tastesport.chat.model.redpacket.RedpacketVO;
import com.tastesport.chat.model.redpacket.RedpacketgetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class RedpacketImpl extends ServiceImpl<RedpacketMapper, Redpacket> implements RedpacketService {

    @Autowired
    RedpacketMapper rpMapper;

    @Autowired
    RedpacketGetMapper rpgMapper;

    //获取用户金币额
    @Override
    public BigDecimal getMoney(String userId) {
        return this.baseMapper.getBeans(userId);
    }

    @Override
    public Integer insert(Redpacket rp) {
        int res = this.baseMapper.insert(rp);
        return rp.getId();
    }

    @Override
    @Transactional
    public Integer databaseProc(Redpacket rp, RedpacketGet rpg) {
        try {
            // 更新此红包的：剩余个数、剩余金额
            this.baseMapper.updateRemain(rp.getId(), rp.getRemainMoney());
            if (rp.getUserId() != null && rp.getType() == 2) { //更新手气最佳
                this.baseMapper.updateBest(rp.getId(), rp.getBestUserid(), rp.getBestMoney());
                this.baseMapper.changeBest(rp.getId(), rp.getUserId(), 0); // 上次手气最佳改回普通
            }
            // 领红包表新增一条数据
            rpgMapper.insert(rpg);
            // 更新领红包人用户的金币总额
            this.baseMapper.changeMoney(rpg.getGetUserid(), rpg.getGetMoney());
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
        return 0;
    }

    @Override
    public Integer changeMoney(String userId, BigDecimal money) {
        return this.baseMapper.changeMoney(userId, money);
    }

    @Override
    public Integer setState(Integer id, Integer state) {
        return this.baseMapper.setState(id, state);
    }

    @Override
    public RedpacketVO getRedpacketById(Integer id) {
        return this.baseMapper.getRedpacketById(id);
    }

    @Override
    public IPage<RedpacketgetVO> userGet(Page page, QueryParam param) {
        QueryWrapper<RedpacketgetVO> query = new QueryWrapper<>();
        if (param.getId() == null) {
            return null;
        }
        query.eq("get_userid", param.getUserId());
        if (param.getStartTime() != null && param.getEndTime() != null) {
            query.and(qw -> qw.apply("get_time BETWEEN " + param.getStartTime() + " AND " + param.getEndTime()));
        } else if (param.getStartTime() != null && param.getEndTime() == null) {
            query.and(qw -> qw.apply("get_time >= " + param.getStartTime()));
        } else if (param.getStartTime() == null && param.getEndTime() != null) {
            query.and(qw -> qw.apply("get_time <= " + param.getEndTime()));
        }
        query.orderByDesc("id");
        return rpgMapper.page(page, query);
    }

    @Override
    public IPage<Redpacket> userSend(Page page, QueryParam query) {
        QueryWrapper<Redpacket> queryWrapper = new QueryWrapper<>();
        if (query.getId() == null) {
            return null;
        }
        queryWrapper.eq("user_id", query.getId());
        if (query.getStartTime() != null && query.getEndTime() != null) {
            queryWrapper.and(qw -> qw.apply("unix_timestamp(send_time) BETWEEN unix_timestamp('" + query.getStartTime() + "') AND unix_timestamp('" + query.getEndTime() + "')"));
        } else if (query.getStartTime() != null && query.getEndTime() == null) {
            queryWrapper.and(qw -> qw.apply("unix_timestamp(send_time) >= unix_timestamp('" + query.getStartTime() + "')"));
        } else if (query.getStartTime() == null && query.getEndTime() != null) {
            queryWrapper.and(qw -> qw.apply("unix_timestamp(send_time) <= unix_timestamp('" + query.getEndTime() + "')"));
        }
        queryWrapper.orderByDesc("id");
        return rpMapper.selectPage(page, queryWrapper);
    }
}
